package com.kyxx.topics.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kyxx.topics.BaseActivity;
import com.kyxx.topics.BaseApp;
import com.kyxx.topics.R;
import com.kyxx.topics.adapter.OnTextChangeAdapter;
import com.kyxx.topics.bean.MemberAddCardBean;
import com.kyxx.topics.bean.MembershipCardBean;
import com.kyxx.topics.net.ResponseListener;
import com.kyxx.topics.net.RestClient;
import com.kyxx.topics.net.UrlUtils;
import com.kyxx.topics.utils.StringUtil;
import com.kyxx.topics.utils.ToastUtils;
import com.kyxx.topics.utils.Tools;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONException;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;

public class JoinMemberActivity extends BaseActivity{
    @Bind(R.id.iv_back)
    ImageView iv_back;
    @Bind(R.id.tv_baocun)
    TextView tv_baocun;
    @Bind(R.id.rl_join_bar)
    RelativeLayout rl_join_bar;
    @Bind(R.id.iv_card_icon)
    ImageView iv_card_icon;
    @Bind(R.id.rl_card_icon)
    RelativeLayout rl_card_icon;
    @Bind(R.id.et_join_member_num)
    EditText et_join_member_num;
    @Bind(R.id.et_join_member_name)
    EditText et_join_member_name;
    @Bind(R.id.et_join_member_phone)
    EditText et_join_member_phone;
    @Bind(R.id.tv_member_shop_name)
    TextView tv_member_shop_name;
    @Bind(R.id.tv_join_member_phone)
    TextView tv_join_member_phone;
    private String IDcard = "";
    private String cartid ="";
    private String mobile ="";
    private String name ="";
    private String sid = "";
    private String type_id="";
    private String code;
    private MemberAddCardBean bean;
    private MembershipCardBean CardBean;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;
    private String Type_logo;
    private String shop_color;
    private String shop_name;
    protected void findViewById() {
    }

    protected void loadViewLayout() {
        setContentView(R.layout.activity_join_member);
        ButterKnife.bind(this);
        bean = (MemberAddCardBean) getIntent().getSerializableExtra("bean");
        CardBean = (MembershipCardBean) getIntent().getSerializableExtra("CardBean");
        if(CardBean!=null){
            type_id = CardBean.getType_id();
            Type_logo = CardBean.getType_logo();
            shop_color = CardBean.getShop_color();
            shop_name = CardBean.getType_name();
        }
        if(bean!=null){
            type_id = bean.getType_id();
            Type_logo = bean.getType_logo();
            shop_color = bean.getShop_color();
            shop_name = bean.getType_name();
        }
        options = new DisplayImageOptions.Builder()
                // 设置图片在下载期间显示的图片
                .showImageOnLoading(R.mipmap.image_default)
                // 设置图片Uri为空或是错误的时候显示的图片
                .showImageForEmptyUri(R.mipmap.image_default)
                // 设置图片加载/解码过程中错误时候显示的图片
                .showImageOnFail(R.mipmap.image_default)
                .cacheInMemory(true)
                // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)
                // 设置下载的图片是否缓存在SD卡中
                .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型
                // .displayer(new RoundedBitmapDisplayer(9999))
                .resetViewBeforeLoading(false)
                .build();

    }


    protected void processLogic() {
        if(StringUtil.isEmpty(BaseApp.getModel().getMobile())){
            et_join_member_phone.setVisibility(View.VISIBLE);
            tv_join_member_phone.setVisibility(View.GONE);
        }else{
            et_join_member_phone.setVisibility(View.GONE);
            tv_join_member_phone.setVisibility(View.VISIBLE);
            tv_join_member_phone.setText(BaseApp.getModel().getMobile());
            mobile = BaseApp.getModel().getMobile();
        }
        imageLoader.displayImage(UrlUtils.BASEHTTP+Type_logo,iv_card_icon, options);
        tv_member_shop_name.setText(shop_name);
        if (shop_color == null || shop_color.contains(",")) {
            shop_color = "FFFDBB2B";
        } else {
            shop_color ="FF" + shop_color;
        }
        int a = new java.math.BigInteger("ffffff", 16).intValue();
        int b = new java.math.BigInteger(shop_color, 16).intValue();
        rl_join_bar.setBackgroundColor(b);
        rl_card_icon.setBackgroundColor(b);
        if(a == b){
            iv_back.setImageResource(R.drawable.ic_arrow_right);
        }else{
            iv_back.setImageResource(R.mipmap.common_title_back);
        }
    }

    protected void setListener() {
        et_join_member_num.addTextChangedListener(new OnTextChangeAdapter(){
            @Override
            public void afterTextChanged(Editable s) {
                cartid = et_join_member_num.getText().toString().trim();
            }
        });
        et_join_member_name.addTextChangedListener(new OnTextChangeAdapter(){
            @Override
            public void afterTextChanged(Editable s) {
                name = et_join_member_name.getText().toString().trim();
            }
        });
        et_join_member_phone.addTextChangedListener(new OnTextChangeAdapter(){
            @Override
            public void afterTextChanged(Editable s) {
                mobile= et_join_member_phone.getText().toString().trim();
            }
        });
    }

    private void regist() {
        RequestParams params = new RequestParams();
        JSONObject obj = new JSONObject();
        obj.put("uid", BaseApp.getModel().getId());
        obj.put("shop_id", sid);
        obj.put("type_id", type_id);
        obj.put("cardnum", cartid);
        obj.put("name", name);
        obj.put("IDcard", IDcard);
        obj.put("mobile", mobile);
        params.put("json", obj.toString());
        RestClient.post(UrlUtils.VIP_CARD_REGIST, params, context, new ResponseListener(this, true) {
            @Override
            public void success(int arg0, Header[] arg1, org.json.JSONObject result) {
                Tools.toast(context, "绑定会员卡成功");
//                startActivity(new Intent(context, MemberDeatailActivity.class).putExtra("type_id", type_id));
                finish();

            }

            @Override
            public void failed(int arg0, Header[] arg1, org.json.JSONObject result) {
                try {
                    if (result.getInt("code") == 1) {
                        ToastUtils.show(JoinMemberActivity.this, result.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    @OnClick({R.id.iv_back, R.id.tv_baocun,R.id.iv_join_member_scan})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                this.finish();
                break;
            case R.id.tv_baocun:
                if(!StringUtil.isEmpty(cartid)){
                    regist();
                }else  if(mobile.length()>0&name.length()>0){
                    if(!StringUtil.isMobileExact(mobile)){
                        ToastUtils.show(context,"请输入正确的手机号");
                        et_join_member_phone.setText("");
                    }else if(!StringUtil.isChineseChar(name)){
                        ToastUtils.show(context,"请输入真实姓名");
                        et_join_member_name.setText("");
                    }else {
                        regist();
                    }
                }else {
                    ToastUtils.show(context,"请输入您的会员卡号或手机号姓名");
                }
                break;
            case R.id.iv_join_member_scan:
                startActivityForResult(new Intent(this,CommonScanActivity.class),1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(data == null){
            return;
        }
        switch (requestCode){
            case 1:
                et_join_member_num.setText(data.getExtras().getString("result"));
                break;
        }
    }
}